<?php
namespace app\shop\controller\v1;
use think\Controller;
use think\Validate;
use think\Db;
use org\BaiduMap;
use think\Model;
use think\Loader;
use org\HMongodb;
use think\Exception;
use org\MongoModel;
class Seller extends Controller
{
    
    /**
     * 析构函数
     */
    // public function __construct() 
    // {
    //     parent::__construct();
    // }

    public function _initialize(){
        $this->SellerModel = Loader::model('Seller');
    }
    public function index(){
        $domian =  $_SERVER['HTTP_HOST'];
        $area = db::name('areas')->where('area_type','=',1)->select();
        $category = db::name('rrb_list')->where('pid',1)->select();
        // $parent_id = 1;
        $this->assign('domian',$domian);
        $this->assign('area',$area);
        $this->assign('category',$category);
        // $this->assign('parent_id',$parent_id);
        echo $this->fetch();
    }

    public function getAreas(){
        $param = input('post.');
        $area = db::name('areas')->where('parent_id','=',$param['id'])->select();
        // foreach($area as $k => $v)
        //     $html .= "<option value='{$v['area_id']}'>{$v['area_name']}</option>";        
        // // exit($html);
        // return $html;
        return $area;
    }

    public function toAuth(){
        $param = input('post.');
        $result = $this->validate($param, 'SellerValidate.auth');
            if (true !== $result) {
                return ['code'=>400,'msg'=>$result,'data'=>[],'status'=>false];
            }
        $mobile = $param['mobile'];
        $shopData = $this->SellerModel->getSellerData($mobile);
        if($shopData){
            return ['data' => [], 'code' => 400, 'msg' => '商户已存在', 'status' => false];
        }
        // $baidu = new BaiduMap();
        // $data['province'] = $param['province'];
        // $data['city'] = $param['city'];
        // $data['district'] = $param['district'];
        // $data['province'] = db::name('areas')->where('area_id',$data['province'])->value('area_name');
        // $data['city'] = db::name('areas')->where('area_id',$data['city'])->value('area_name');
        // $data['district'] = db::name('areas')->where('area_id',$data['district'])->value('area_name');
        // $data['address'] = $param['address'];
        // $add_name = $data['province'].$data['city'].$data['district'].$param['address'];
        // $coordinate = $baidu::getCoordinateByName($add_name);
        // if(is_array($coordinate)){
        //     if($coordinate['result']['location']['lng']){
        //         $lng = $coordinate['result']['location']['lng'];
        //         $data['lng'] = $lng;
        //     }
        //     if($coordinate['result']['location']['lat']){
        //         $lat = $coordinate['result']['location']['lat'];
        //         $data['lat'] = $lat;
        //     }
        // }
        $lng = $param['lng'];
        $lat = $param['lat'];
        if($lng && $lat){
            $citycode = $baidu::getCodeByPoint($lng, $lat);
            $data['citycode'] = $citycode['citycode'];
            $data['adcode'] = $citycode['adcode'];
            $data['cid'] = $baidu::getCidByPoint($lng,$lat);
        }
            $data['add_time'] = time();
            $data['salt'] =  md5(rand(1111,9999));
            $data['password'] = md5($data['salt'].'slzd'.$mobile);
            $data['role_id'] = 7;
            $data['status'] = 1;
            $data['category_id'] = $param['category_id'];
            $data['user_name'] = $mobile;
            $data['nav_list'] = '商品列表|goods.php?act=list,订单列表|order.php?act=list,用户评论|comment_manage.php?act=list,会员列表|users.php?act=list,商店设置|shop_config.php?act=list_edit';
            $data['shop_name'] = $param['shopname'];
            $data['audit'] = 0;
            $data['is_order'] = 1;
            $data['lng'] = $lng;
            $data['lat'] = $lat;
            $data['o_start_time'] = strtotime(date('Ymd').'8:00');
            $data['o_end_time'] = strtotime(date('Ymd').'20:00');
        $auth = new \app\shop\model\Auth();
        Db::startTrans();
        try{
            // if($shopData){
            //     $seller_add = $this->SellerModel->allowField(true)->save($data,['admin_id'=>$shopData['admin_id']]);
            //     // $admin = $this->SellerModel->getSellerData($mobile);
            //     // $auth_data['admin_id'] = $admin['admin_id'];
            //     $auth_data['idNumber'] = $param['idNumber'];
            //     $auth_data['manage'] = $param['manage'];
            //     $auth_data['sfzzm'] = $param['sfzzm'];
            //     $auth_data['mobile'] = $param['mobile'];
            //     $auth_data['sfzfm'] = $param['sfzfm'];
            //     $auth_data['license'] = $param['license'];
            //     $auth_data['created_at'] = time();
            //     $auth_data['updated_at'] = time();
            //     $auth_result = $auth->allowField(true)->save($auth_data,['admin_id'=>$shopData['admin_id']]);
            // }else{
                // $seller_add = $this->SellerModel->allowField(true)->save($data,['admin_id'=>$shopData['admin_id']]);
                $seller_add = $this->SellerModel->insert($data);
                $admin = $this->SellerModel->getSellerData($mobile);
                $auth_data['admin_id'] = $admin['admin_id'];
                $auth_data['idNumber'] = $param['idNumber'];
                $auth_data['manage'] = $param['manage'];
                $auth_data['sfzzm'] = $param['sfzzm'];
                $auth_data['mobile'] = $param['mobile'];
                $auth_data['sfzfm'] = $param['sfzfm'];
                $auth_data['license'] = $param['license'];
                $auth_data['created_at'] = time();
                $auth_data['updated_at'] = time();
                $auth_result = $auth->insert($auth_data);
                $info['id'] = $admin['admin_id'];
                $info['coordinate']['lng'] = (float)$lng;// 经度
                $info['coordinate']['lat'] = (float)$lat;// 纬度
                $mongo = new MongoModel('slzd_pro', config('mongodbConf.seller'));
                $res = $mongo->add($info);// 新增
            // }
            if($seller_add && $auth_result && $res){
               Db::commit();
            }else{
                throw new Exception("Error");
            }
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            // return ['data' => [], 'code' => 400, 'msg' => '提交失败', 'status' => false];
            return $e->getMessage();
        }

        $resultData = $this->SellerModel->getSellerData($mobile);
        if($resultData){
            return ['data' => [], 'code' => 200, 'msg' => '提交成功', 'status' => true]; 
        }
        
    }
}